package com.zh.springcloud.interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.omg.PortableInterceptor.Interceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @Description:  feign 拦截器，处理转发请求时，将请求参数进行转发
 * @ClassName FeginHeaderInterceptor
 * @date: 2022.03.28 11:09
 * @Author: zhanghang
 */

@Slf4j
@Configuration
public class FeginHeaderInterceptor implements RequestInterceptor {
		@Override
		public void apply(RequestTemplate template) {
			ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder
					.getRequestAttributes();
			HttpServletRequest request = attributes.getRequest();
			Enumeration<String> headerNames = request.getHeaderNames();
			if (headerNames != null) {
				while (headerNames.hasMoreElements()) {
					String name = headerNames.nextElement();
					String values = request.getHeader(name);
					template.header(name, values);
				}
			}
			Enumeration<String> bodyNames = request.getParameterNames();
			StringBuffer body =new StringBuffer();
			if (bodyNames != null) {
				while (bodyNames.hasMoreElements()) {
					String name = bodyNames.nextElement();
					String values = request.getParameter(name);
					body.append(name).append("=").append(values).append("&");
				}
			}
			if(body.length()!=0) {
				body.deleteCharAt(body.length()-1);
				template.body(body.toString());
			}
		}

}
